<template>
  <div class="role">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>筛选搜索</span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="活动名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="mini"
              :disabled="isLoading"
              @click="findName">查询搜索</el-button>
            <el-button
              size="mini"
              :disabled="isLoading"
              @click="resetTable">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button
        class="btn_add"
        size="mini"
        @click="addRoleFnc"
        :disabled="isLoading">添加角色</el-button>
      <el-table
        :data="roleData"
        border
        style="width: 100%"
        v-loading="isLoading">
        <el-table-column
          prop="id"
          label="编号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          >
        </el-table-column>
        <el-table-column
          prop="createdTime"
          label="添加时间">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="{row}">
            <el-button
              size="mini"
              type="text"
              :disabled="isLoading"
              @click="menuEdit(row)">分配菜单</el-button>
            <el-button
              size="mini"
              type="text"
              :disabled="isLoading"
              @click="sourceEdit(row)">分配资源</el-button>
             <el-button
              size="mini"
              type="text"
              :disabled="isLoading"
              @click="handleEdit(row)">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              :disabled="isLoading"
              @click="handleDelete(row)">删除</el-button>
           </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="form.current"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="form.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      :disabled="isLoading">
    </el-pagination>
    <add-role
      :dialogVisible="dialogVisible"
      :isEdit="isEdit"
      :c_id="c_id"
      @watchChild="closePage" />
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { deleteRole, getAllRole, getRolePages, SaveRoles } from '@/services/role'
import AddRole from '@/views/role/components/AddRole.vue'
import { Form } from 'element-ui'
export default Vue.extend({
  name: 'LoginIndex',
  components: {
    AddRole
  },
  data () {
    return {
      roleData: [],
      form: {
        name: '',
        current: 1,
        size: 5
      },
      dialogVisible: false,
      isEdit: false,
      // c_name: '',
      // c_code: '',
      // c_description: '',
      c_id: 0,
      isLoading: true,
      totalCount: 0
    }
  },
  created () {
    this.loadAllRole()
  },
  methods: {
    async loadAllRole () {
      this.isLoading = true
      // const { data } = await getAllRole()
      const { data } = await getRolePages(this.form)
      // console.log('data', data)

      if (data.code === '000000') {
        this.roleData = data.data.records
        this.totalCount = data.data.total
        this.isLoading = false
      }
      // console.log('dataa', data)
    },
    // 添加角色
    addRoleFnc () {
      this.dialogVisible = true
      this.isEdit = false
    },
    handleEdit (row:SaveRoles) {
      this.dialogVisible = true
      this.isEdit = true
      // this.c_name = row.name
      // this.c_code = row.code
      // this.c_description = row.description
      this.c_id = row.id as number
    },
    handleDelete (row:SaveRoles) {
      this.$confirm('此操作将删除一项分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteRole(row.id as number)
        if (data.code === '000000') {
          this.$message.success('删除成功')
          this.loadAllRole()
        }
      }).catch(() => {
        this.$message.error('删除失败')
      })
    },
    closePage () {
      this.dialogVisible = false
      this.loadAllRole()
    },
    menuEdit (row:SaveRoles) {
      this.$router.push({ name: 'alloc-menu', query: { roleId: row.id?.toString() } })
    },
    sourceEdit (row:SaveRoles) {
      this.$router.push({ name: 'alloc-resource', query: { roleId: row.id?.toString() } })
    },
    handleSizeChange (val:number) {
      this.form.size = val
      this.form.current = 1
      this.loadAllRole()
    },
    handleCurrentChange (val:number) {
      this.form.current = val
      this.loadAllRole()
    },
    findName () {
      this.form.current = 1
      this.loadAllRole()
    },
    resetTable () {
      (this.$refs.form as Form).resetFields()
      this.form.current = 1
      this.loadAllRole()
    }
  }
})
</script>

<style lang="scss" scoped>
.btn_add{
  margin-bottom: 2rem;
}
.el-form{
  margin-top: 2rem;
  .el-form-item{
    display: inline-block;
  }
}
// .el-form-item:nth-child(2){
//   margin-left: 30rem;
// }
</style>

function getAllRole() {
  throw new Error('Function not implemented.')
}
